const Client = require('@alicloud/docmind-api20220711');
const Util = require('@alicloud/tea-util');
const fs = require('fs');

const getResult = async (params) => {
    console.log("文档转换提交控制开始：\n", params.files);

    // 使用默认凭证初始化Credentials Client
    // const cred = new Credential.default();
    const client = new Client.default({
        // 访问的域名，支持ipv4和ipv6两种方式，ipv6请使用docmind-api-dualstack.cn-hangzhou.aliyuncs.com
        endpoint: 'docmind-api.cn-hangzhou.aliyuncs.com',
        // 通过credentials获取配置中的AccessKey ID
        accessKeyId: "LTAI5t6ReNzjiSkBv4HFS3HK",
        // 通过credentials获取配置中的AccessKey Secret
        accessKeySecret: "WDFgnQtvO9qRMwWB9bGSxYJp5JfWwG",
        type: 'access_key',
        regionId: 'cn-hangzhou'
    });

    const advanceRequest = new Client.SubmitConvertPdfToWordJobAdvanceRequest();
    const file = fs.createReadStream(params.files.filepath);
    advanceRequest.fileUrlObject = file;
    advanceRequest.fileName = 'example.pdf';
    const runtimeObject = new Util.RuntimeOptions({});
    const response = await client.submitConvertPdfToWordJobAdvance(advanceRequest, runtimeObject);
    return response.body;
}


//pdf转word
async function sumbitPdfToWord(file) {
    try {
        const res = await getResult(file)
        console.log("文档转换结果：\n",)
        return {
            code: 0,
            msg: "删除成功",
            data: res.SubmitConvertPdfToWordJobResponseBody.data.SubmitConvertPdfToWordJobResponseBodyData
        }
    } catch (error) {
        console.log("文档转换提交控制错误：", error);
        return {
            code: 1,
            msg: "任务提交失败",
            error: error
        }
    }
}



// SubmitConvertPdfToWordJobResponseBody {
//     data: SubmitConvertPdfToWordJobResponseBodyData {
//       id: 'docmind-20250502-217559d0d15a49ff8707f8a45a77b848'
//     },
//     requestId: 'FC6E633D-ED01-5AB9-9C0B-C87148054D00'
//   }


//查询文档转换结果
async function queryResult(id) {
    try {
        // 使用默认凭证初始化Credentials Client
        // const cred = new Credential.default();
        const client = new Client.default({
            // 访问的域名，支持ipv4和ipv6两种方式，ipv6请使用docmind-api-dualstack.cn-hangzhou.aliyuncs.com
            endpoint: 'docmind-api.cn-hangzhou.aliyuncs.com',
            // 通过credentials获取配置中的AccessKey ID
            accessKeyId: "LTAI5t6ReNzjiSkBv4HFS3HK",
            // 通过credentials获取配置中的AccessKey Secret
            accessKeySecret: "WDFgnQtvO9qRMwWB9bGSxYJp5JfWwG",
            type: 'access_key',
            regionId: 'cn-hangzhou'
        });

        const resultRequest = new Client.GetDocumentConvertResultRequest();
        resultRequest.id = id;
        const response = await client.getDocumentConvertResult(resultRequest);
        console.log("查询文档转换结果：\n", response.body.data[0]);
        return {
            code: 0,
            msg: "查询成功",
            data: response.body.data[0]
        }
        // return response.body;
    } catch (error) {
        console.log("查询文档转换错误：", error);
        return {
            code: 1,
            msg: "任务提交失败",
            error: error
        }
    }
}
// queryResult("docmind-20250502-217559d0d15a49ff8707f8a45a77b848")

module.exports = {
    sumbitPdfToWord,
    queryResult

}